package com.sea.human.controller;

import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerGroup;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.power.service.PowerGroupService;
import com.codecrane.core.util.DateUtils;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.human.entity.Performance;
import com.sea.human.service.PerformanceService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 *  控制器
 * @author 陈鸿飞
 *  <br/>
 *  <br/>
 */
@Controller
@RequestMapping("/sea/human/performance/manage")
public class PerformanceManageController {

    @Autowired
    private PerformanceService performanceService;
    @Autowired
    private PowerGroupService powerGroupService;

    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        model.addAttribute("powerGroup",powerGroupService.findByCnd(null));
        return "/sea/human/performanceManage/main";
    }

    @RequestMapping("/talk")
    public String talk(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("performance",performanceService.findById(id));
        return "/sea/human/performanceManage/talk";
    }
    /**
     * 新增主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(Model model,@RequestParam("id")Integer id) {
        PowerUser user = (PowerUser) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER_SESSION);
        PowerGroup powerGroup = powerGroupService.findGroupByUserId(Integer.parseInt(user.getId().toString()));
        model.addAttribute("user",user);
        model.addAttribute("powerGroup",powerGroup);
        model.addAttribute("performanceDate",DateUtils.getCurrentDateTime("yyyy-MM"));
        model.addAttribute("performanceId",id);
        return "/sea/human/performance/add";
    }


    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     Performance performance) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<Performance> performanceList = performanceService.findByCnd(performance);
        PageInfo<Performance> page = new PageInfo<>(performanceList);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(@RequestParam("performanceDate")String performanceDate) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            PowerUser user = (PowerUser) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER_SESSION);
            PowerGroup powerGroup = powerGroupService.findGroupByUserId(Integer.parseInt(user.getId().toString()));
            if (null != user && null !=powerGroup && performanceService.save(user,powerGroup,
                    performanceDate) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (DuplicateKeyException e){
            result.setMsg("下月绩效已存在,请勿重复提交!");
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 保存
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public AjaxReturn check(Performance performance) {
        AjaxReturn result = Ajax.fail().setMsg("提交失败！");
        try{
            if (null != performance && performanceService.usave(performance) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (RuntimeException e){
            result.setMsg(e.getMessage());
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }


}
